package io.renren.sf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.exception.RRException;
import io.renren.common.utils.Download;
import io.renren.sf.dao.CampusListDao;
import io.renren.sf.entity.CityEntity;
import io.renren.sf.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import io.renren.sf.dao.CampusDao;
import io.renren.sf.entity.CampusEntity;
import io.renren.sf.service.CampusService;
import org.springframework.util.StringUtils;


@Service("campusService")
public class CampusServiceImpl extends ServiceImpl<CampusDao, CampusEntity> implements CampusService {

    @Autowired
    private CampusListDao campusListDao;
    @Autowired
    private CityService cityService;

    @Override
    public Page<CampusEntity> selectCampusEntityPage(Map<String, Object> params) {
        String campusName = (String) params.get("campusName");
        String pcaid = (String) params.get("pcaid");
        long page = Long.parseLong((String) params.get("page"));
        long limit = Long.parseLong((String) params.get("limit"));
        Page<CampusEntity> campusPage = new Page<>(page, limit);
        QueryWrapper<CampusEntity> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(campusName)){
            queryWrapper.like("sc.campus_name",campusName);
        }
        if(!StringUtils.isEmpty(pcaid)){
            queryWrapper.and(w -> w.eq("sc.cid",pcaid));
        }
        campusListDao.selectCampusEntityPage(campusPage,queryWrapper);
        return campusPage;
    }

    @Override
    public Integer insertssq(List<String> city) throws RRException {
        if(city.size() < 3){
            throw new RRException("请选择省市区三层,而不是两层");
        }
        // 首先获取省市区jsonObject
        JSONObject jsonObject = Download.readJsonFile(); // 总省json
        // 1.获取省份/直辖市
        String province = city.get(0);
        JSONObject jsonObject1 = jsonObject.getJSONObject(province); // 省name, child json
        // 省份名称
        String provinceName = jsonObject1.getString("name");
        CityEntity provinceEntity = new CityEntity();
        provinceEntity.setId(Integer.parseInt(province));
        provinceEntity.setParentid(0);
        provinceEntity.setCityName(provinceName);
        if(cityService.getById(Integer.parseInt(province)) == null){
            // 不存在省份才添加
            cityService.save(provinceEntity);
        }
        // 2.获取市
        String shi = city.get(1);
        JSONObject jsonObject2 = jsonObject1.getJSONObject("child"); // 省下的总市json
        JSONObject jsonObject3 = jsonObject2.getJSONObject(shi); // 市name, child json
        String shiName = jsonObject3.getString("name");
        CityEntity shiEntity = new CityEntity();
        shiEntity.setId(Integer.parseInt(shi));
        shiEntity.setParentid(Integer.parseInt(province));
        shiEntity.setCityName(shiName);
        if(cityService.getById(Integer.parseInt(shi)) == null){
            cityService.save(shiEntity);
        }
        // 3.获取区/县
        String region = city.get(2);
        JSONObject jsonObject4 = jsonObject3.getJSONObject("child"); // 市下的区或县的json
        String regionName = jsonObject4.getString(region);
        CityEntity regionEntity = new CityEntity();
        regionEntity.setId(Integer.parseInt(region));
        regionEntity.setParentid(Integer.parseInt(shi));
        regionEntity.setCityName(regionName);
        if(cityService.getById(Integer.parseInt(region)) == null){
            cityService.save(regionEntity);
        }
        return regionEntity.getId();
    }

    @Override
    public Map<String, Integer> getCampus(Long campusId) {
        return baseMapper.getCampus(campusId);
    }
}